package de.ur.iw.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SQLHelper {

	Connection con;

	public void connect(String location, String username, String password)
			throws InstantiationException, IllegalAccessException,
			ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		con = DriverManager.getConnection("jdbc:mysql://" + location, username,
				password);
	}

	public void createDataBase(String name) throws SQLException {
		Statement stmt = con.createStatement();
		stmt.execute("CREATE DATABASE IF NOT EXISTS " + name);
		stmt.close();
	}

	public void command(String sqlQuery) throws SQLException {
		Statement stmt = con.createStatement();
		stmt.execute(sqlQuery);
		stmt.close();
	}

	public ResultSet query(String sqlQuery) throws SQLException {
		Statement stmt = con.createStatement();
		ResultSet result = stmt.executeQuery(sqlQuery);
		stmt.close();
		return result;
	}

	public boolean close() {
		try {
			con.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

}
